<?php
/**
 * 房间管理
 * User: @671
 * Date: 2018/8/24 0024
 * Time: 16:34
 */
namespace app\publics\controller;

use think\facade\Request;
use think\Db;
use thinkcms\auth\library\Tree;
use thinkcms\auth\model\Menu;

use app\common\model\TableModel;
use app\common\model\UserModel;

use app\common\utils\StatusUtils;


class Table extends Base
{
    protected $urt_model;

    public function __construct()
    {
        parent::__construct();
        $this->urt_model = new TableModel();
        $this->u_model = new UserModel();
    }

    // 列表
    public function index()
    {
        $param = $this->urt_model->param_format(input('param.'));

        $map = [];

        if(!empty($param['id'])){
            $map['urt.id'] = $param['id'];
        }
        $map['ur.status'] = 0;
        $count = (int)$this->urt_model
            ->alias('urt')
            ->leftJoin('p_user u', 'urt.uid = u.uid')
            ->rightJoin('p_user_room ur','urt.rid = ur.rid')
            ->where($map)
            ->count();


        $list  = $this->urt_model
            ->alias('urt')
            ->leftJoin('p_user u', 'urt.uid = u.uid')
            ->rightJoin('p_user_room ur','urt.rid = ur.rid')
            ->where($map)
            ->field('urt.*, u.uid,u.phone,u.username,ur.name')
            ->order('urt.create_time asc')
            ->paginate($param['limit'], $count);


        $cur_page_sum =  $this->urt_model
            ->alias('urt')
            ->leftJoin('p_user u', 'urt.uid = u.uid')
            ->rightJoin('p_user_room ur','urt.rid = ur.rid')
            ->where('ur.status',0)
            ->count();

        $all_page_sum = $this->urt_model->count();

        $this->assign([
            'param' => $param,
            'list'  => $list,
            'cur_page_sum' =>$cur_page_sum,
            'all_page_sum' =>$all_page_sum,
            'limit' => select_limit($param['limit']),
            'count' => $count,
            'page'  => $list->render()
        ]);
        return $this->fetch();
    }

    //删除
    public function delete()
    {
        $post = input('param.');

        if (empty($post['id'])) {
            $this->error('至少选择一个用户');
        }

        $this->urt_model->destroy($post['id']);
        $this->success('删除成功');
    }
}
